<?php

namespace App\Console\Commands;

use App\Models\Period;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;

class BuildPeriod extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'build:period';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Build Period daily';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info('going to build period of this month');
        $now = Carbon::now();
        $period = [
            'name'=>$now->year.'-'.str_pad($now->month,2,'0',STR_PAD_LEFT).' 期',
            'year'=>$now->year,
            'month'=>$now->month,
            'start_date'=>$now->firstOfMonth(),
            'end_date'=>Carbon::now()->lastOfMonth()
        ];

        Period::firstOrCreate(Arr::only($period,['year','month']), $period);

        $this->info('build period finished');

    }
}
